﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class BUserGroupM : INTPage
{
    SqlHelp help = new SqlHelp();
    public int rowNo = 0;
    CommonMethod comHelp = new CommonMethod();

    protected void Page_Load(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl2);
        if (!IsPostBack)
        {
            WebGroupBind(Session["UserCode"].ToString());
        }
    }

    #region 初始化绑定列表数据 WebsBind(string UserCode)
    protected void WebGroupBind(string UserCode)
    {   
        //权限
        comHelp.GetRight(this, new string[] { Session["UserCode"].ToString(), Session["CoCode"].ToString(), "BUserGroup" },
            GrvUserGroup, new string[] { "cmdAdd", "cmdEdit" }, new int[] { 4, 5 }, CommonModel.loginOut2);

        QueryBind(0);
    }
    #endregion

    #region 换页
    protected void GrvUserGroup_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            if (Session["UserCode"] != null)
            {
                GrvUserGroup.PageIndex = e.NewPageIndex;
                QueryBind(Convert.ToInt16(HidQueryFlag.Value.ToString()));
            }
        }
        catch
        { }
    }
    #endregion

    //Gridview 行事件
    protected void GrvUserGroup_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        rowNo++;
        //鼠标经过事件
        if (e.Row.RowIndex != -1)
        {
            //设置悬浮鼠标指针形状为"小手"
            e.Row.Attributes["style"] = "Cursor:hand";
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "lastColor=this.style.backgroundColor;this.style.backgroundColor='#CCCC99';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=lastColor;");
                ((LinkButton)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "return confirm('确实要删除？');");                
            }
        }
    }

    #region 查询
    protected void cmdQuery_Click(object sender, EventArgs e)
    {
        QueryBind(1);
        HidQueryFlag.Value = "1";
    }
    //
    protected void QueryBind(int n)
    {
        string StrSql = " select UG.UGCode,UG.UGName,UG.UGDesc "
            + "from B_UserGroup UG where UG.DeleteFlag='0' ";

        if (n == 1)
        {
            if (!string.IsNullOrEmpty(txtUGCode.Text.ToString()))
            { StrSql += string.Format(" and UG.UGCode like '%{0}%' ", txtUGCode.Text.ToString()); }
            if (!string.IsNullOrEmpty(txtUGName.Text.ToString()))
            { StrSql += string.Format(" and UG.UGName like '%{0}%'", txtUGName.Text.ToString()); }
            if (!string.IsNullOrEmpty(txtUGDesc.Text.ToString()))
            { StrSql += string.Format(" and UG.UGDesc like '%{0}%'", txtUGDesc.Text.ToString()); }
        }
        StrSql += " order by UG.UGCode DESC ";
        DataTable Dt = help.GetDataTable(StrSql);
        GrvUserGroup.DataSource = Dt;
        GrvUserGroup.DataBind();
        //ADD Sunwen 2009-10-22
        if (Dt.Rows.Count > 0)
        {
            // 设置分页行永远显示
            GrvUserGroup.BottomPagerRow.Visible = true;
            //赋值Label记录条数
            Label label1 = GrvUserGroup.BottomPagerRow.FindControl("lblCount") as Label;
            label1.Text = Dt.Rows.Count.ToString();
        }
    }
    #endregion

    #region 添加
    protected void cmdAdd_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl2);
        if (string.IsNullOrEmpty(txtUGCode.Text.ToString()) || string.IsNullOrEmpty(txtUGName.Text.ToString()))
        { CommonMethod.regAlertMessage("编号、名称都不可以为空!", this); }
        else
        {
            //统计列表中与要添加的类型相同的个数
            int ComN = 0;
            //
            string StrGvNSql = string.Format("select count(*) from B_UserGroup where UGCode='{0}'", txtUGCode.Text.ToString());
            ComN = Convert.ToInt16(help.RunSqlReturn(StrGvNSql));
            if (ComN < 1)
            {
                //获取当前用户名称
                string UserName = Session["UserCode"].ToString();
                //当前时间
                string StrDate = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm");

                string StrInsert = string.Format("Insert Into B_UserGroup(UGCode,UGName,UGDesc,DeleteFlag,AddAuthor,"
                    + "AddDate,UpdateAuthor,UpdateDate) Values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}') ", txtUGCode.Text.ToString(), txtUGName.Text.ToString(), txtUGDesc.Text.ToString(), "0", UserName, StrDate, UserName, StrDate);
                //添加结果标志
                int result = help.RunSqlInt(StrInsert);
                if (result > 0)
                {
                    CommonMethod.regAlertMessage("操作成功-Success!", this);
                    QueryBind(0);
                    HidQueryFlag.Value = "0";
                    txtUGCode.Focus();
                }
                else
                {
                    CommonMethod.regAlertMessage("出现错误-Error!", this);
                    txtUGCode.Focus();
                }
            }
            else { CommonMethod.regAlertMessage(txtUGCode.Text.ToString() + "  已经存在！", this); }
        }
    }
    #endregion
    #region 编辑
    protected void cmdEdit_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl2);
        string StrUpdate = string.Format(" Update B_UserGroup set UGName='" + txtUGName.Text.ToString() + "',UGDesc='{0}' where UGCode='{1}' ", txtUGDesc.Text.ToString(), txtUGCode.Text.ToString());
        if (help.RunSqlInt(StrUpdate) > 0)
        {
            CommonMethod.regAlertMessage("更新成功！", this);
            QueryBind(0);
            HidQueryFlag.Value = "0";
            cmdAdd.Enabled = true;
            cmdEdit.Enabled = false;
            txtUGCode.Enabled = true;
            txtUGCode.Text = "";
            txtUGDesc.Text = "";
            txtUGName.Text = "";
        }
        else { CommonMethod.regAlertMessage("更新失败！", this); }
    }
    #endregion

    #region GridView 行事件
    //行删除
    protected void GrvUserGroup_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl2);

        //获取并判断此项在用户与用户组中是否存在
        string UGCode = GrvUserGroup.Rows[e.RowIndex].Cells[1].Text.Trim().ToString();
        int ComN = Convert.ToInt16(help.RunSqlReturn(string.Format("select count(*) from B_UserAndGroup where UGCode='{0}'", UGCode)));
        if (ComN < 1)
        {
            string StrDelete = string.Format(" Delete from B_UserGroup where UGCode='{0}'", UGCode);
            if (help.RunSqlInt(StrDelete) > 0)
            {
                CommonMethod.regAlertMessage("删除成功！", this);
                QueryBind(Convert.ToInt16(HidQueryFlag.Value.ToString()));
            }
            else
            {
                CommonMethod.regAlertMessage("更新失败！", this);
            }
        }
        else
        {
            CommonMethod.regAlertMessage(UGCode + " 此存在于用户组，现在不可以删除，要删除请先到用户与用户组关联删除此相关关联！", this);
        }
    }
    //行编辑
    protected void GrvUserGroup_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtUGCode.Text = GrvUserGroup.Rows[GrvUserGroup.SelectedIndex].Cells[1].Text.Trim().ToString();
        txtUGCode.Enabled = false;
        txtUGName.Text = GrvUserGroup.Rows[GrvUserGroup.SelectedIndex].Cells[2].Text.Trim().ToString();
        txtUGDesc.Text = GrvUserGroup.Rows[GrvUserGroup.SelectedIndex].Cells[3].Text.Trim().ToString();

        cmdAdd.Enabled = false;
        cmdEdit.Enabled = true;
    }
    #endregion

    
    protected void cmdClear_ServerClick(object sender, EventArgs e)
    {
        cmdAdd.Enabled = true;
        cmdEdit.Enabled = false;
        txtUGCode.Enabled = true;
        txtUGCode.Text = "";
        txtUGDesc.Text = "";
        txtUGName.Text = "";
    }
    //跳页
    protected void GrvUserGroup_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "NewPage")
            {
                TextBox tb = GrvUserGroup.BottomPagerRow.FindControl("txtNewPageIndex") as TextBox;
                int pageindex = Convert.ToInt32(tb.Text);
                GrvUserGroup.PageIndex = pageindex - 1;
                //数据绑定
                QueryBind(Convert.ToInt16(HidQueryFlag.Value.ToString()));
            }
        }
        catch
        {
            CommonMethod.regAlertMessage("输入不合法-Error format!", this);
        }
    }
}
